package cn.template.config;

import cn.template.enums.UserRole;
import cn.template.models.Company;
import cn.template.models.User;
import cn.template.services.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Initializer {
    private UserService repository;
    private static final Logger logger = LogManager.getLogger();
    public Initializer(UserService repository) {
        this.repository = repository;
    }


    public boolean InitializeData() {
        if (this.repository.count() == 0) {
            logger.info("initializing system since database is empty");
            return this.addUser();
        }
        return false;
    }

    private boolean addUser() {
        logger.info("adding a super company with admin account");
        Company company = new Company("xx科技有限公司", "广东", "广州市", "天河区xx大厦2501", "admin", "13831881234");
        User user = new User("admin", "1380010", "admin");
        user.addRole(UserRole.SuperAdmin);
        user.setPassword(UserService.encodePassword("12345678"));
        user.setCompany(company);
        this.repository.save(user);
        return true;
    }

}
